package com.itheima.hmshopping.app.controller;

import com.itheima.hmshopping.app.service.PmsBrandService;
import com.itheima.hmshopping.entity.PmsBrand;
import com.itheima.hmshopping.util.HmPage;
import com.itheima.hmshopping.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;


    /**
     * 获取全部品牌列表
     *
     * @return
     */
    @GetMapping("/listAll")
    public R getListAll() {
        List<PmsBrand> brandList = pmsBrandService.getListAll();
        return R.succeed(brandList, "");
    }

    /**
     * 根据品牌名称分页获取品牌列表
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("/list")
    public R getList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, String keyword) {
        HmPage pageList = pmsBrandService.getList(pageNum, pageSize, keyword);
        return R.succeed(pageList, "");
    }


    /**
     * 根据编号查询品牌信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable(value = "id") Integer id) {
        PmsBrand pmsBrand = pmsBrandService.getById(id);
        return R.succeed(pmsBrand, "");
    }


    /**
     * 添加品牌
     *
     * @param pmsBrand
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody PmsBrand pmsBrand) {
        pmsBrandService.save(pmsBrand);
        return R.succeed("", "");
    }


    /**
     * 更新品牌
     *
     * @param id
     * @param brand
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateById(@PathVariable("id") Integer id, @RequestBody PmsBrand brand) {
        pmsBrandService.updateById(id, brand);
        return R.succeed("", "");
    }


    /**
     * 删除品牌
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public R deleteById(@PathVariable("id") Integer id){
        pmsBrandService.deleteById(id);
        return R.succeed("","");
    }


    /**
     * 批量删除品牌
     * @param ids
     * @return
     */
    @PostMapping("/delete/batch")
    public R deleteBatch(@PathVariable("ids") Integer [] ids){
        pmsBrandService.deleteByIds(ids);
        return R.succeed("", "");
    }


    /**
     * 批量更新显示状态
     * @param ids
     * @param showStatus
     * @return
     */
    @PostMapping("/update/showStatus")
    public R updateStatus(@PathVariable("ids") Integer [] ids,Integer showStatus){
        pmsBrandService.updateStatus(ids,showStatus);
        return R.succeed("","");
    }


    /**
     * 批量更新厂家制造商状态
     * @param factoryStatus
     * @return
     */
    @PostMapping("/update/factoryStatus")
    public R updateFactoryStatus(@PathVariable("ids")Integer [] ids,Integer factoryStatus){
        pmsBrandService.updateFactoryStatus(ids,factoryStatus);
        return R.succeed("", "");
    }
}